package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsGrouponRules;
import com.qf.service.DtsGrouponRulesService;
import com.qf.service.DtsGrouponService;
import com.qf.util.JacksonUtil;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * @author Cc
 * @version 1.0
 *
 *      团购规则
 */

@RestController
@CrossOrigin("*")
@RequestMapping("/admin/groupon")
public class AdminGrouponController {

    @Autowired
    private DtsGrouponService dtsGrouponService;

    @Autowired
    private DtsGrouponRulesService dtsGrouponRulesServiceService;

    /**
     *    查询列表
     *      接口地址 : http://localhost:8083/admin/groupon/list?page=1&limit=20&sort=add_time&order=desc
     * @return
     */

    @RequiresPermissions("admin:groupon:list")
    @RequiresPermissionsDesc(menu = {"推广管理", "团购规则"}, button = "查询")
    @GetMapping("/list")
    public Object list(String sort, String order,String goodsId,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit){

        IPage<DtsGrouponRules> storageIPage = dtsGrouponRulesServiceService.page(goodsId,page, limit, sort, order);

        HashMap map = new HashMap();
        map.put("total", storageIPage.getTotal());
        map.put("items", storageIPage.getRecords());

        return ResponseUtil.ok(map);
    }


    /**
     *      团购添加
     *      接口地址 : http://localhost:8083/admin/groupon/create
     */
    @RequiresPermissions("admin:groupon:create")
    @RequiresPermissionsDesc(menu = {"推广管理", "团购规则"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody String body) throws ParseException {
        DtsGrouponRules grouponRules = new DtsGrouponRules();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        grouponRules.setGoodsId(new Long(JacksonUtil.parseInteger(body,"goodsId")))
                    .setGoodsName(JacksonUtil.parseString(body,"goodsName"))
//                    .setPicUrl(JacksonUtil.parseString(body,"picUrl"))
                    .setDiscount(new BigDecimal(JacksonUtil.parseInteger(body,"discount")))
                    .setDiscountMember(JacksonUtil.parseInteger(body,"discountMember"))
                    .setAddTime(new Date())
                    .setUpdateTime(new Date())
                    .setExpireTime(format.parse(JacksonUtil.parseString(body,"expireTime")));

            //TODO  传参数有问题
        HashMap map = new HashMap();
         dtsGrouponRulesServiceService.add(grouponRules);

        return  ResponseUtil.ok(grouponRules);
    }


    /**
     *      删除团购规则
     *  接口地址 : http://localhost:8083/admin/groupon/delete
     */
    @RequiresPermissions("admin:groupon:delete")
    @RequiresPermissionsDesc(menu = {"推广管理", "团购规则"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsGrouponRules grouponRules){
        Integer id = grouponRules.getId();
        dtsGrouponRulesServiceService.deleteGrouponRules(id);
        return ResponseUtil.ok();
    }
}
